package com.xxxt.wsc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxt.entity.Summarize;
import com.xxxt.wsc.service.SummarizeService;

/**
 * @description:总结的接口
 * @author:wsc
 * @date:2020年6月12日 下午8:17:53
 */
@RestController
@RequestMapping("/summarize/")
public class SummarizeController {

	@Autowired
	SummarizeService summarizeService;
	
	//查询总结信息
	@GetMapping("get-summarize")
	public JSONObject getSummarize(String id) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "查询成功";
		Summarize summarize = null;
		try {
			summarize = summarizeService.querySummarizeById(id);
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "查询失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		json.put("status", status);
		json.put("message", message);
		json.put("summarize", summarize);
		return json;
	}
	//查询总结列表
	@GetMapping("get-summarize-list")
	public JSONObject getSummarizeList(String classGradeId,String commitTime,String sno, String summarizeType,String tno,int pageNum,int pageSize,HttpSession session) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "查询成功";
		//如果是学生登录了账号，查询自己的
		List<Summarize> list;
		try {
			PageHelper.startPage(pageNum, pageSize);
			list = summarizeService.querySummarizeByOthers(classGradeId, commitTime, sno, summarizeType,tno);
			PageInfo<Summarize> pageInfo = new PageInfo<Summarize>(list);
			json.put("status", status);
			json.put("message", message);
			json.put("pageInfo", pageInfo);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "查询失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
	}
	//提交总结
	@PostMapping("add-summarize")
	public JSONObject addSummarize(String title, String content, String summarizeType,HttpSession session) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "提交成功";
		boolean b;
		try {
			b = summarizeService.addSummarize(title, content, summarizeType, (String)session.getAttribute("account"));
//			b = summarizeService.addSummarize(title, content, summarizeType, "117333440134");
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "提交失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if(b) {
			json.put("status", status);
			json.put("message", message);
		}else {
			status = 1;
			message = "提交失败";
			json.put("status", status);
			json.put("message", message);
		}
		return json;
	}
	//修改总结
	@PostMapping("update-summarize")
	public JSONObject updateSummarize(String id,String title, String content, String summarizeType,HttpSession session) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "修改成功";
		boolean b;
		try {
			b = summarizeService.updateSummarize(id, title, content, summarizeType, (String)session.getAttribute("account"));
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "修改失败";
			json.put("status", status);
			json.put("message", message);
			e.printStackTrace();
			return json;
		}
		if(b) {
			json.put("status", status);
			json.put("message", message);
		}else {
			status = 1;
			message = "修改失败";
			json.put("status", status);
			json.put("message", message);
		}
		return json;
	}
	//删除总结
	@PostMapping("delete-summarize")
	public JSONObject deleteSummarize(String id) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "删除成功";
		boolean b = false;
		try {
			b = summarizeService.deleteSummarize(id);
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "删除失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if(b) {
			json.put("status", status);
			json.put("message", message);
		}else {
			status = 1;
			message = "删除失败";
			json.put("status", status);
			json.put("message", message);
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}
	
	//下载单个学生单个月报/临时总结
	@GetMapping("downloadOne")
	public ResponseEntity<byte[]> downloadOne(String summarizeId, String tno,HttpServletResponse response,HttpServletRequest request) throws Exception {    
//		request.setCharacterEncoding("UTF-8");
		//生成并下载文件
		ResponseEntity<byte[]> entity = summarizeService.downloadOne(response, request, summarizeId, tno);   
	    return entity;
	}
	
	//下载打包的月报/总结
	@GetMapping("downloadAll")
	public ResponseEntity<byte[]> downloadAll(HttpServletResponse response,HttpServletRequest request,String ids, String tno) throws IOException {    
//		request.setCharacterEncoding("UTF-8");
		//生成并下载文件
		ids = ids.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\"", "");
		ResponseEntity<byte[]> entity = summarizeService.downloadAll(response, request, ids, tno);
	    return entity;
	}
}
